import * as apis from '../services/patentDataService.js'
import constant from '../constant/constant'
import { message } from 'antd'

export default {
    namespace: 'patentData',

    state: {
        patentDataTableList: [],
        selectedIds: [],
        selectedRows: [],
        regionalDistributionChartData: [],
        yearAnalysisChartData: [],
        mechanismChartData: {
            companyData: [],
            schoolData: []
        },
        htmlData: "",
        technologyHotspot: "",
        abstractList: [],
        technologyClusteringList: {},
        total: 0,
        pageSize: 10,
        current: 1,
        queryParam: {}
    },

    reducers: {
        setPatentDataTableList: setPatentDataTableList,
        setSelectedIds: setSelectedIds,
        setSelectedRows: setSelectedRows,
        setRegionalDistributionChartData: setRegionalDistributionChartData,
        setYearAnalysisChartData: setYearAnalysisChartData,
        setMechanismChartData: setMechanismChartData,
        setAbstractList: setAbstractList,
        setTechnologyClustering: setTechnologyClustering,
        setTotal: setTotal,
        setPagination: setPagination,
        setQueryParam: setQueryParam
    },

    effects: {
        getPatentDataTableList: getPatentDataTableList,
        deletePatentById: deletePatentById,
        uploadPatentData: uploadPatentData,
        getTechnologyHotspot: getTechnologyHotspot,
        getTechnologyClustering: getTechnologyClustering,
        getPatentDataSearch: getPatentDataSearch

    }
}

function setQueryParam(state, payload) {
    let _state = JSON.parse(JSON.stringify(state));
    _state.queryParam = payload.data
    return _state
}

function setPagination(state, payload) {
    let _state = JSON.parse(JSON.stringify(state));
    _state.pageSize = payload.data.pageSize
    _state.current = payload.data.current
    return _state
}

function setAbstractList(state, payload) {
    let _state = JSON.parse(JSON.stringify(state));
    _state.abstractList = payload.data
    return _state
}

function setMechanismChartData(state, payload) {
    let _state = JSON.parse(JSON.stringify(state));
    _state.mechanismChartData = payload.data
    return _state
}

function setYearAnalysisChartData(state, payload) {
    let _state = JSON.parse(JSON.stringify(state));
    _state.yearAnalysisChartData = payload.data
    return _state
}

function setRegionalDistributionChartData(state, payload) {
    let _state = JSON.parse(JSON.stringify(state));
    _state.regionalDistributionChartData = payload.data
    return _state
}

function setSelectedRows(state, payload) {
    let _state = JSON.parse(JSON.stringify(state));
    _state.selectedRows = payload.data
    return _state
}

function setSelectedIds(state, payload) {
    let _state = JSON.parse(JSON.stringify(state));
    _state.selectedIds = payload.data
    return _state
}


function setPatentDataTableList(state, payload) {
    let _state = JSON.parse(JSON.stringify(state));
    _state.patentDataTableList = payload.data
    return _state
}
//技术聚类分析
function setTechnologyClustering(state, payload) {
    let _state = JSON.parse(JSON.stringify(state));
    _state.technologyClusteringList = payload.data
    return _state
}

function setTotal(state, payload) {
    let _state = JSON.parse(JSON.stringify(state));
    _state.total = payload.data
    return _state
}
/**
 * 获取专利数据列表
 */
function* getPatentDataTableList(payload, { select, call, put }) {
    let { pageSize, current } = yield select(state => state.patentData)

    let result = yield call(apis.getPatentDataTableList, {
        limit: pageSize,
        page: current
    })

    yield put({
        type: "patentData/setPatentDataTableList",
        data: result.data.data
    })

    yield put({
        type: "patentData/setTotal",
        data: result.data.total
    })
}

/**
 * 单个录入专利数据
 */
function* uploadPatentData(payload, { select, call, put }) {
    let result = yield call(apis.uploadPatentData, payload.data)

    yield put({
        type: "app/setServerRes",
        data: result.data
    })

    yield put({
        type: "app/initServerRes"
    })

    yield put({
        type: "patentData/getPatentDataTableList"
    })
}

/**
 * 通过Id删除专利数据
 */
function* deletePatentById(payload, { select, call, put }) {
    let result = yield call(apis.deletePatentById, payload.data)

    yield put({
        type: "app/setServerRes",
        data: result.data
    })

    if (result.data.code == 200) {
        yield put({
            type: "app/initServerRes"
        })

        yield put({
            type: "patentData/getPatentDataTableList"
        })
    }

    yield put({
        type: "patentData/setDeletedIds",
        data: []
    })
}

/**
 * 获取技术热点html
 */
function* getTechnologyHotspot(payload, { select, call, put }) {
    let result = yield call(apis.getTechnologyHotspot, payload.data)
    if (result) {
        window.open(constant.serverFileUrl + "/patentanalysis/" + result.data.data.filename, "_blank")
    }
}

/**
 * 获取技术聚类html
 */
function* getTechnologyClustering(payload, { select, call, put }) {
    let result = yield call(apis.getTechnologyClustering, payload.data)
    if (result) {
        yield put({
            type: "patentData/setTechnologyClustering",
            data: result.data.data
        })
    }
}

/**
 * 根据条件查询专利数据
 */
function* getPatentDataSearch(payload, { select, call, put }) {
    let { pageSize,current,queryParam } = yield select(state => state.patentData)

    let query = {
        ...queryParam,
        limit: pageSize,
        page: current
    }

    let result = yield call(apis.getPatentDataSearch, query)
    if (result) {
        if (result.data.msg == 'Data Not Ready') {
            yield put({
                type: 'setTotal',
                data: 0
            })

            yield put({
                type: 'setPatentDataTableList',
                data: []
            })
        }

        if (result.data.data.length > 0) {
            yield put({
                type: 'setTotal',
                data: result.data.total
            })

            yield put({
                type: 'setPatentDataTableList',
                data: result.data.data
            })
        }
    }
}
